<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" name="roleName"
                   placeholder="请输入角色名称" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色CODE</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" name="roleCode"
                   placeholder="请输入角色CODE" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色状态</label>
        <div class="layui-input-block">
            <input checked="checked" lay-filter="switchTest" lay-skin="switch" lay-text="启用|停用" name="status" id="status"
                   type="checkbox">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">菜单权限：</label>
        <div class="layui-input-block">
            <div class="layui-btn-container" style="margin-top: 10px">
                <button class="layui-btn layui-btn-xs" lay-demo="setChecked" type="button">全选</button>
                <button class="layui-btn layui-btn-xs layui-btn-danger" lay-demo="reload" type="button">全不选
                </button>
            </div>
            <div style="width: 500px;border:1px solid #eee">
                <div class="demo-tree-more" id="menuTree" style="margin: 10px"></div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            util = layui.util,
            $ = layui.$;

        let menuTree = [];
        // 初始化给tree赋值
        $(window).load(function(){
            $.ajax({
                url: '/role/menuTree',
                type: 'GET',
                contentType: 'application/json; charset=UTF-8',
                dataType: "json",
                success: function (res) {
                    menuTree = res.idList;  //全选集合
                    //基本演示
                    tree.render({
                        elem: '#menuTree'
                        , data: res.treeList
                        , showCheckbox: true  //是否显示复选框
                        , id: 'menuTree'
                        , isJump: true //是否允许点击节点时弹出新窗口跳转
                        , click: function (obj) {

                        }
                    });
                }
            })
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定提交吗", {
                title: '提示'
            }, function () {
                if ($("#status").is(':checked')) {
                    data.field.status = '1';
                } else {
                    data.field.status = '0';
                }

                let thisData = data.field;
                let checkedData = tree.getChecked('menuTree'); //获取选中节点的数据
                let treeData = [];
                treeData.push(getChecked_list(checkedData));
                let endData = {
                    status: thisData.status,
                    roleName: thisData.roleName,
                    roleCode: thisData.roleCode,
                    treeList: treeData
                };

                $.ajax({
                    url: '/role/addRole',
                    type: 'POST',
                    contentType: 'application/json; charset=UTF-8',
                    dataType: "json",
                    data:JSON.stringify(endData),
                    success: function (data) {
                        if (data.code == 200){
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            window.parent.location.reload();
                        }else{
                            var alert=layer.alert(data.message,{
                                title: "提示",
                            },function (){
                                // 关闭弹出层
                                layer.close(index);
                                layer.close(alert);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        }

                    }
                })

            });

            return false;
        });

        /**
         * 获取勾选的数据
         * @param data
         * @returns {string}
         */
        function getChecked_list(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }

        //按钮事件
        util.event('lay-demo', {
            setChecked: function () {
                tree.setChecked('treeId', treeId); //勾选指定节点
            }
            , reload: function () {
                //重载实例
                tree.reload('treeId', {});
            }
        });

    });
</script>
</body>
</html>